import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter_section_table_view/flutter_section_table_view.dart';
import 'package:ls_work/a_yyt/models/pending_selected_group_entity.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/res/styles.dart';
import 'package:ls_work/util/toast.dart';

import '../../util/flat_button.dart';



class PendingSingleGroupPage extends StatefulWidget {
  List<PendingSelectedGroupEntity> sortList;
  final Function(
    PendingSelectedGroupEntity groupInfo,
  )? callBack;
  PendingSingleGroupPage({Key? key, required this.sortList,this.callBack});
  @override
  _PendingSingleGroupPageState createState() =>
      _PendingSingleGroupPageState();
}

class _PendingSingleGroupPageState extends State<PendingSingleGroupPage> {

  PendingSelectedGroupEntity? _groupInfo;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('选择'),
        elevation: 0.5,
        actions: <Widget>[
          FlatsButton(
            onPressed: () {
              if (_groupInfo == null) {
                Toast.show('请选择分组');
                return;
              }
              if(widget.callBack!=null){
                widget.callBack!(_groupInfo!);
              }
              Navigator.pop(context);
            },
            child: Text(
              '确定',
              style: TextStyle(
                color: Colours.app_main,
                fontSize: 18,
              ),
            ),
          )
        ],
      ),
      body: SectionTableView(
        sectionCount: 1,
        numOfRowInSection: (section) => widget.sortList.length,
        cellAtIndexPath: (section, row) {
          return Container(
            height: 60.0,
            padding: EdgeInsets.only(left: 30, right: 30),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text(
                  ' ${widget.sortList[row].name}',
                  style: TextStyles.textDark18,
                ),
                Checkbox(
                  value: widget.sortList[row].isSelected,
                  onChanged: (value) {
                    setState(() {
                      widget.sortList[row].isSelected = value;
                      if (value==true) {
                        _groupInfo = widget.sortList[row];
                        for (PendingSelectedGroupEntity info
                            in widget.sortList) {
                          if (info.name != widget.sortList[row].name) {
                            info.isSelected = false;
                          }
                        }
                      } else{
                        _groupInfo = null;
                      }
                    });
                  },
                )
              ],
            ),
          );
        },
        headerInSection: (section) {
          return Container(
            height: 64.0,
            padding: EdgeInsets.only(left: 30),
            color: Colours.bg_color,
            alignment: Alignment.centerLeft,
            child: Text(
              ' ${widget.sortList.first.serviceTypeName}',
              style: TextStyles.textDark20,
            ),
          );
        },
        divider: Container(
          color: Colours.line,
          height: 1.0,
        ),
      ),
    );
  }
}
